﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IGolovkine.Core.Diagnostics;
using System.Text.RegularExpressions;

namespace IGolovkine.ShoutCastRipper
{
    public class ContentTitleDateExtractor : IContentTitleDateExtractor
    {
        private readonly IEnumerable<string> _datePatterns;

        public ContentTitleDateExtractor(IEnumerable<string> datePatters)
        {
            Debug.NotDefault(()=>datePatters);
            _datePatterns = datePatters;

        }

        public bool TryGetContentDate(string contentTitle, out DateTime result)
        {
            foreach (string datePattern in _datePatterns)
            {
                Match match = Regex.Match(contentTitle, datePattern);
                if (match.Captures.Count == 1)
                {
                    return DateTime.TryParse(match.Captures[0].Value, out result);
                }
            }

            result = DateTime.MaxValue;
            return false;
        }
    }
}
